import router from '@/router'
import api from '@/http/api'

export default {
  namespaced: true,
  state: {
    config: {}
  },
  mutations: {
    SET_CONFIG (state, val) {
      state.config = val
      document.title = val.tenantName
      document.documentElement.style.setProperty('--color-primary', `#${val.frontPrimaryColor}`)
      document.documentElement.style.setProperty('--color-primary-light', `#${val.frontPrimaryColorLight}`)
      sessionStorage.setItem('tenantConfig', JSON.stringify(val));
    },
    HAS_CONFIG (state) {}
  },
  actions: {
    loadRongchangConfig ({ commit }) {
      const tenantCode = sessionStorage.getItem('tenantCode') || '1731596052865302530'
      if (tenantCode) {
        api.getTenantCodeConfig(tenantCode).then(res => {
          if (!res.code) {
            commit('SET_CONFIG', res.data)
          }
        })
      }
    },
    hasConfig ({ commit, dispatch }) {
      const tenantConfig = sessionStorage.getItem('tenantConfig');
      if (tenantConfig) {
        commit('SET_CONFIG', JSON.parse(tenantConfig))
      } else {
        dispatch('loadRongchangConfig')
      }
    },
  }
}
